//
//  DynamicModel.m
//  WeCommunity
//
//  Created by mac on 2017/6/14.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "DynamicModel.h"


@implementation DynamicModel

-(long)customReadTotalNum
{
    if (self.readTotalNum == 0) {
        
        long contentNum = 0;
        long timeNum = 0;
        
//        NSDate * nowTime = [NSDate date];
        long nowTime =  [[NSDate date] timeIntervalSince1970];
        long dataTime = [self.createDate timeIntervalSince1970];
        long value = 60 * 60; //时间间距 1小时
        if (nowTime - dataTime > value) {
            //数据的时间距离现在 > 1小时
            //用户发送的内容里 每一个文字+2 阅读量 每一张图片+20 阅读量
            contentNum = self.content.length * 2 + self.imageList.count * 20;

            //距离现在第一个小时内 每分钟 +10
            timeNum += 600;
            //距离现在除了第一个小时内 每小时 +1
            timeNum += (nowTime - 3600 - dataTime) / 3600 * 10;
            
        }else{
            //数据的时间距离现在 <= 1小时 ( 1小时内最大可能阅读量:400 + 90 + 600 = 1090  最小阅读量：1+ 600 = 601)
            //用户发送的内容里 每一个文字+1 阅读量 每一张图片+10 阅读量
            contentNum = self.content.length * 1 + self.imageList.count * 10;
            //距离现在每分钟 +10
            timeNum = (nowTime - dataTime) / 60 * 10;
            
        }
        _customReadTotalNum = 0;
        _customReadTotalNum += contentNum;
        _customReadTotalNum += timeNum;
    }else{
        _customReadTotalNum = self.readTotalNum;
    }
    return _customReadTotalNum;
}

-(NSMutableArray *)likeList
{
    if (!_likeList) {
        _likeList = [NSMutableArray array];
    }
    return _likeList;
}
-(long)likeTotalNum
{
    return (float)self.likeList.count;
}

-(BOOL)isLike
{
    if (![UserInfoManger shared].isLogin) {
        return NO;
    }
    for (NSString * objectId in self.likeList) {
        if ([objectId isEqualToString:[UserInfoManger shared].contact.objectId]) {
            return YES;
        }
    }
    return NO;
}

-(BOOL)isReport
{
    for (NSString * objectId in self.reportList) {
        if ([objectId isEqualToString:[UserInfoManger shared].contact.objectId]) {
            return YES;
        }
    }
    return NO;
}

-(NSString *)formatTime1
{
    _formatTime1 = [TimeStampTool formatDateWithDynamicTime:self.createTime];
    return _formatTime1;
}

-(NSString *)formatTime2
{
    _formatTime2 = [TimeStampTool formatTime:self.createTime WithType:@"MM.dd"];
    return _formatTime2;
}

-(NSString *)formatLikeTotalNum
{
    if (self.likeTotalNum  < 10000) {
        _formatLikeTotalNum = [NSString stringWithFormat:@"%ld",self.likeTotalNum];
    }else if (self.likeTotalNum  >= 10000){
        _formatLikeTotalNum = [NSString stringWithFormat:@"%.1lfW",self.likeTotalNum / 10000.0];
    }
    return _formatLikeTotalNum;
}


-(CGFloat)contentWidth
{
    if (!_contentWidth) {
        _contentWidth = KScreen_Width * 0.683;
    }
    return _contentWidth;
}


-(CGFloat)contentHeight
{
    CGFloat baseHeight = 0; //基础固定高度
    
    //文字高度
    YYLabel * tempLabel = [CountHeightShared shared].yyLabel;
    tempLabel.font = PFR_FONT(14);
    tempLabel.text = self.content;
    tempLabel.lineBreakMode = NSLineBreakByCharWrapping;
    tempLabel.numberOfLines = 4;
    CGSize fourLineSize =  [tempLabel sizeThatFits:CGSizeMake(self.contentWidth, CGFLOAT_MAX)];
    CGFloat  fourLineHeight = fourLineSize.height;
    
    tempLabel.numberOfLines = 0;
    CGSize allTextSize =  [tempLabel sizeThatFits:CGSizeMake(self.contentWidth, CGFLOAT_MAX)];
    CGFloat allTextHeight = allTextSize.height;
    
    if (allTextHeight > fourLineHeight) {
        if (self.showAlltext) {
            baseHeight += allTextHeight + 25;
        }else{
            baseHeight += fourLineHeight + 25;
        }
    }else{
        baseHeight += allTextHeight;
    }
    
    if (self.imageList.count  > 0) {
        //图片高度
        CGFloat width = (self.contentWidth - 12) / 3;
        CGFloat height = 5 ;
        if (self.imageList.count == 1) {
            height += KScreen_Width * 0.32;
        }else{
            height += width + (self.imageList.count - 1) / 3 * (width + 6);
        }
        baseHeight += height;
    }
    _contentHeight = baseHeight;

    return _contentHeight;
}

-(CGFloat)totalHeight
{
    _totalHeight = 0;
    CGFloat baseHeight = 80; //基础固定高度
    
    _totalHeight += baseHeight;
    _totalHeight += self.contentHeight;
    
    return _totalHeight;
}

-(CGFloat)mineContentHeight
{
    if(!_mineContentHeight){
        CGFloat baseHeight = 18; //基础固定高度
        
        if (self.imageList.count > 0) {
            baseHeight += KScreen_Width * 0.27;
        }else if (self.content.length > 0) {
            //文字高度
            YYLabel * tempLabel = [CountHeightShared shared].yyLabel;
            tempLabel.font = PFR_FONT(14);
            tempLabel.text = self.content;
            tempLabel.lineBreakMode = NSLineBreakByCharWrapping;
            tempLabel.numberOfLines = 3;
            CGSize size =  [tempLabel sizeThatFits:CGSizeMake(KScreen_Width - 104, 64)];
            
            baseHeight += size.height;
        }
        
        baseHeight += 10;
        
        _mineContentHeight += baseHeight;
        
        NSLog(@"%lf",_mineContentHeight);
    }
    return _mineContentHeight;
}

+(DynamicModel *)setValueFormObject:(id)object
{
    DynamicModel * model = [DynamicModel new];
    model.dataType = [[object objectForKey:@"dataType"] intValue];
    model.objectId = [object objectForKey:@"objectId"];
    model.contactId = [object objectForKey:@"contactId"];
    model.content = [object objectForKey:@"content"];
    model.readTotalNum = [[object objectForKey:@"readTotalNum"] floatValue];
    model.reportList = [object objectForKey:@"reportList"];
    model.reportCount = [[object objectForKey:@"reportCount"] integerValue];
    model.likeList = [object objectForKey:@"likeList"];
    model.createDate = [object objectForKey:@"createdAt"];
    model.createTime = [NSString stringWithFormat:@"%.0lf",[model.createDate timeIntervalSince1970] * 1000];
    //图片
    model.imageList = [NSMutableArray array];
    for (AVFile * file in (NSArray *)[object objectForKey:@"imageList"]) {
        if (![file isEqual: [NSNull null]]  && file.url.length > 0) {
            [model.imageList addObject:file.url];
        }
    }
    //用户信息
    model.contact = [ContactModel setValueFormObject:[object objectForKey:@"contact"]];
    
    return model;
}



@end
